//JavaScript代码区域
var current_page = 1;
var field = {};
var KZQ = '/admin/train/';

//获取搜索条件的值
function fieldValue() {
    field.base_name = $('input[name = base_name]').val().trim();
    field.v_train_date = $('input[name=v_train_date]').val();
    field.v_train = $('input[name=v_train]').val().trim();
    field.line_date = $('input[name=line_date]').val();
    field.v_tnum = $('input[name=v_tnum]').val().trim();
    field.train_date = $('input[name=train_date]').val();
    field.train = $('input[name=train]').val().trim();
    return field;
}

layui.use(['element', 'table', 'upload', 'layer', 'laytpl', 'laydate'], function () {
    var $ = layui.$,
        element = layui.element,
        table = layui.table,
        upload = layui.upload,
        layer = layui.layer,
        laytpl = layui.laytpl,
        laydate = layui.laydate;
    laydate.render({
        elem: '#v_train_date' //指定元素
    });
    laydate.render({
        elem: '#linedate' //指定元素
    });
    laydate.render({
        elem: '#traindate' //指定元素
    });
    //方法级渲染
    var tableIns = table.render({
        elem: '#LAY_table_user',
        url: '/admin/train/today',
        method: 'POST',
        where: fieldValue(),
        cols: [
            [
                {field: 'id', title: 'ID', width: 100, sort: true, fixed: false, align: 'center'},
                {field: 'v_train_date', title: '车组首发日期', width: 120, align: 'center'},
                {field: 'v_train', title: '车组', width: 110, align: 'center'},
                {field: 'line_date', title: '车底首发日期', width: 120, align: 'center'},
                {field: 'v_tnum', title: '车底', width: 110, align: 'center'},
                {field: 'thisday', title: '车次运行日期', width: 120, align: 'center'},
                {field: 'train', title: '车次', width: 100, align: 'center'},
                {field: 'is_open', title: '业务开关', minWidth: 150, templet: '#is_open'},
                {field: 'check_train', title: '动检', width: 70, align: 'center'},
                {field: 'trip', title: '趟次', width: 70, align: 'center'},
                {field: 'day_num', title: '第几天', width: 80, align: 'center'},
                {field: 'start_station', title: '始发站', width: 80, align: 'center'},
                {field: 'end_station', title: '终点站', width: 80, align: 'center'},
                {field: 'start_time', title: '始发时间', width: 90, align: 'center'},
                {field: 'end_time', title: '到站时间', width: 90, align: 'center'},
                {field: 'station_num', title: '站点数量', width: 90, align: 'center'},
                {field: 'time_length', title: '时长', width: 70, align: 'center'},
                {field: 'mileage', title: '总里程', width: 80, align: 'center'},
                {field: 'train_model', title: '车型', width: 70, align: 'center'},
                {field: 'carriage_num', title: '车厢', width: 70, align: 'center'},
                {field: 'base_name', title: '所属基地', sort: false, width: 90, align: 'center'},
                {field: 'train_type', title: '车次类型', sort: false, width: 90, align: 'center'},
                {fixed: 'right', title: '操作', width: 250, toolbar: '#barTable', align: 'center'}
            ]
        ],
        page: {
            curr: current_page
        },
        limit: 10,
        height: '500px',
        done: function (res, curr, count) {
            current_page = curr;//保存当前页码
        }
    });
    //监听工具条
    table.on('tool(file)', function (obj) {
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        console.log(obj)
        if (layEvent === 'detail') { //查看
            var url = "/admin/train/timetable_day" + "?id=" + data.id;
            layer.open({
                type: 2,
                title: '时刻表',
                shadeClose: true,
                shade: 0.8,
                area: ['80%', '100%'],
                content: url //iframe的url
            });
        } else if (layEvent === 'del') { //删除
            layer.confirm('真的删除行么', function (index) {
                obj.del(); //删除对应行（tr）的DOM结构
                layer.close(index);  //向服务端发送删除指令

            });
        } else if (layEvent === 'edit') { //编辑
            var url = "/admin/train/today_edit?id=" + data.id;
            layer.open({
                type: 2,
                title: '时刻表',
                shadeClose: true,
                shade: 0.8,
                area: ['80%', '100%'],
                content: url //iframe的url
            });
        } else if (layEvent === 'team') {
            var url = "/admin/train/today_team?id=" + data.id;
            layer.open({
                type: 2,
                title: data.train + '班组',
                shadeClose: true,
                shade: 0.8,
                area: ['80%', '100%'],
                content: url //iframe的url
            });
        } else if (layEvent === 'forbidden') {
            var status, status_text;
            if (data.status == 0) {
                status = 1;
                status_text = '启用';
            } else {
                status = 0;
                status_text = '禁用';
            }
            var isLoad;
            var url = KZQ + 'today_forbidden';
            $.ajax({
                url: url,
                method: "post",
                data: {
                    'id': data.id,
                    'status': status
                },
                beforeSend: function () {
                    isLoad = layer.load(0, {
                        shade: false
                    });
                },
                success: function (result) {
                    layer.msg(result.msg);
                    if (result.code === 0) {
                        table_reload(current_page);
                    }
                },
                complete: function () {
                    layer.close(isLoad)
                },
                error: function (error) {
                    console.log(error)
                }
            });
        }
    });

    function table_reload(current_page) {
        var field = fieldValue();
        tableIns.reload({
            where: field,
            method: 'POST',
            page: {
                curr: current_page //重新从第 1 页开始
            }
        });
    }

    //搜索项
    $('#search').click(function () {
        table_reload(1);
    });
    //清除
    $('#clear').click(function () {
        $('.search-box-flex').find('.layui-input-inline').each(function (index, element) {
            $(element).find('input[type=text]').val('');
            $(element).find('select').val('');
        })
        var field = fieldValue();
        tableIns.reload({
            where: field,
            page: {
                curr: 1 //重新从第 1 页开始
            }
        });
    });
    $("#today").click(function () {
        var isLoad;
        $.ajax({
            url: '{:url("init/createtoday")}',
            method: "post",
            beforeSend: function () {
                isLoad = layer.load(0, {
                    shade: false
                }); //0代表加载的风格，支持0-2
            },
            success: function (result) {
                layer.msg(result.msg);
                if (result.code === 0) {
                    tableIns.reload();
                }
            },
            complete: function () {
                layer.close(isLoad)
            },
            error: function (error) {
                console.log(error)
            }
        });
    });
    $('#no-timetable').click(function () {
        var url = KZQ + 'timetable_day_no_lists';
        layer.open({
            type: 2,
            area: ['100%', '100%'],
            fixed: false, //不固定
            maxmin: true,
            content: url
        });
    });
    $('#train-day-repeat').click(function () {
        var url = KZQ + 'today_repeat';
        layer.open({
            type: 2,
            area: ['100%', '100%'],
            fixed: false, //不固定
            maxmin: true,
            content: url
        });
    });
});